package com.zyj.receive.service.impl;

import java.util.Date;
import java.util.List;

import com.zyj.assets.domain.AssetsInfo;
import com.zyj.assets.mapper.AssetsInfoMapper;
import com.zyj.common.core.text.Convert;
import com.zyj.common.utils.DateUtils;
import com.zyj.common.utils.StringUtils;
import com.zyj.persion.domain.AssetsPresion;
import com.zyj.persion.mapper.AssetsPresionMapper;
import com.zyj.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.zyj.receive.mapper.AssetsReceiveMapper;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.service.IAssetsReceiveService;

/**
 * 资产领用Service业务层处理
 * 
 * @author zhuyujie
 * @date 2022-05-09
 */
@Service
public class AssetsReceiveServiceImpl implements IAssetsReceiveService 
{
    @Autowired
    private AssetsReceiveMapper assetsReceiveMapper;


    @Autowired
    private AssetsInfoMapper assetsInfoMapper;

    @Autowired
    private AssetsPresionMapper assetsPresionMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询资产领用
     * 
     * @param receiveId 资产领用主键
     * @return 资产领用
     */
    @Override
    public AssetsReceive selectAssetsReceiveByReceiveId(Long receiveId)
    {
        return assetsReceiveMapper.selectAssetsReceiveByReceiveId(receiveId);
    }

    /**
     * 查询资产领用列表
     * 
     * @param assetsReceive 资产领用
     * @return 资产领用
     */
    @Override
    public List<AssetsReceive> selectAssetsReceiveList(AssetsReceive assetsReceive)
    {
        return assetsReceiveMapper.selectAssetsReceiveList(assetsReceive);
    }

    @Override
    public List<AssetsReceive> selectAssetsReceivePredictedList(AssetsReceive assetsReceive)
    {
        return assetsReceiveMapper.selectAssetsReceivePredictedList(assetsReceive);
    }

    /**
     * 查询资产领用列表-领用和借用
     *
     * @param assetsReceive 资产领用和借用
     * @return 资产领用和借用
     */
    @Override
    public List<AssetsReceive> selectAssetsReceiveBorrowList(AssetsReceive assetsReceive)
    {
        return assetsReceiveMapper.selectAssetsReceiveBorrowList(assetsReceive);
    }

    /**
     * 新增资产领用
     * 
     * @param assetsReceive 资产领用
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetsReceive(AssetsReceive assetsReceive)
    {
        assetsReceive.setCreateTime(DateUtils.getNowDate());
        int rows = 0;
        //insertAssetsInfo(assetsReceive);
        List<AssetsInfo> list = assetsReceive.getAssetsInfoList();
        if(list!=null && !list.isEmpty()){
            for(int i=0;i<list.size();i++) {
                AssetsInfo info = list.get(i);
                String assetsid = info.getId();
                assetsReceive.setAssetsId(info.getId());
                assetsReceive.setAssetsName(info.getAssetsName());
                assetsReceive.setAssetsSn(info.getAssetsSn());
                rows += assetsReceiveMapper.insertAssetsReceive(assetsReceive);
                //修改设备领用状态
                AssetsInfo infonew = new AssetsInfo();
                infonew.setId(assetsid);
                infonew.setAssetsStatus(assetsReceive.getReceiveType());
                assetsInfoMapper.updateAssetsInfo(infonew);
                //入人员资产中间表
                AssetsPresion apersion = new AssetsPresion();
                apersion.setUserId(assetsReceive.getUserId());
                apersion.setAssetsId(assetsid);
                apersion.setCreateBy(assetsReceive.getCreateBy());
                apersion.setCreateTime(new Date());
                assetsPresionMapper.insertAssetsPresion(apersion);
            }
        }
        return rows;
    }


    /**
     * 新增资产报废
     *
     * @param assetsReceive 资产报废
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAssetsReceiveScrap(AssetsReceive assetsReceive)
    {
        assetsReceive.setCreateTime(DateUtils.getNowDate());
        int rows = 0;
        //insertAssetsInfo(assetsReceive);
        List<AssetsInfo> list = assetsReceive.getAssetsInfoList();
        if(list!=null && !list.isEmpty()){
            for(int i=0;i<list.size();i++) {
                AssetsInfo info = list.get(i);
                String assetsid = info.getId();
                assetsReceive.setAssetsId(info.getId());
                assetsReceive.setAssetsName(info.getAssetsName());
                assetsReceive.setAssetsSn(info.getAssetsSn());
                rows += assetsReceiveMapper.insertAssetsReceive(assetsReceive);
                //修改设备领用状态
                AssetsInfo infonew = new AssetsInfo();
                infonew.setId(assetsid);
                infonew.setAssetsStatus("4");
                assetsInfoMapper.updateAssetsInfo(infonew);
                //删除人员设备
                assetsPresionMapper.deleteAssetsPresionByAssetsId(assetsid);
            }
        }
        return rows;
    }

    /**
     * 修改资产领用
     * 
     * @param assetsReceive 资产领用
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetsReceive(AssetsReceive assetsReceive)
    {
        assetsReceiveMapper.deleteAssetsInfoById(assetsReceive.getReceiveId());
        insertAssetsInfo(assetsReceive);
        return assetsReceiveMapper.updateAssetsReceive(assetsReceive);
    }

    /**
     * 资产归还入库
     * 
     * @param receiveIds 资产归还入库
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsReceiveByReceiveIds(String receiveIds,String createby)
    {
        String[] ids = Convert.toStrArray(receiveIds);
        if(ids!=null && ids.length>0){
            for(int i=0;i<ids.length;i++){
                //设备编号
                String assetsid = ids[i];
                System.out.println(assetsid);
                //获取设备信息
                AssetsInfo info = assetsInfoMapper.selectAssetsInfoById(assetsid);
                //获取领用人信息
                long userid = assetsPresionMapper.selectAssetsPresionByAssetsId(assetsid);
                String username = sysUserMapper.selectUserById(userid).getUserName();
                System.out.println(assetsid+"|"+userid+"|"+username);
                //插入一个设备领用
                AssetsReceive assetsReceive = new AssetsReceive();
                assetsReceive.setAssetsId(assetsid);
                assetsReceive.setAssetsName(info.getAssetsName());
                assetsReceive.setAssetsSn(info.getAssetsSn());
                assetsReceive.setUserId(userid);
                assetsReceive.setUserName(username);
                assetsReceive.setReceiveTime(new Date());
                assetsReceive.setReceiveNo("BA" + assetsid);
                assetsReceive.setReceiveType("3");
                assetsReceive.setCreateBy(createby);
                assetsReceive.setCreateTime(new Date());
                assetsReceiveMapper.insertAssetsReceive(assetsReceive);
                //删除人员设备
                assetsPresionMapper.deleteAssetsPresionByAssetsId(assetsid);
                //修改设备状态到在库
                AssetsInfo infonew = new AssetsInfo();
                infonew.setId(assetsid);
                infonew.setAssetsStatus("0");
                assetsInfoMapper.updateAssetsInfo(infonew);
            }
        }
//        assetsReceiveMapper.deleteAssetsInfoByIds(Convert.toStrArray(receiveIds));
        return 1;
    }

    /**
     * 删除资产领用信息
     * 
     * @param receiveId 资产领用主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAssetsReceiveByReceiveId(Long receiveId)
    {
        assetsReceiveMapper.deleteAssetsInfoById(receiveId);
        return assetsReceiveMapper.deleteAssetsReceiveByReceiveId(receiveId);
    }

    /**
     * 新增资产信息
     * 
     * @param assetsReceive 资产领用对象
     */
    public void insertAssetsInfo(AssetsReceive assetsReceive)
    {
        List<AssetsInfo> assetsInfoList = assetsReceive.getAssetsInfoList();
        Long receiveId = assetsReceive.getReceiveId();
        if (StringUtils.isNotNull(assetsInfoList))
        {
            List<AssetsInfo> list = new ArrayList<AssetsInfo>();
            for (AssetsInfo assetsInfo : assetsInfoList)
            {
                assetsInfo.setId(receiveId+"");
                list.add(assetsInfo);
            }
            if (list.size() > 0)
            {
                assetsReceiveMapper.batchAssetsInfo(list);
            }
        }
    }
}
